﻿using DevComponents.DotNetBar.Metro;
using PlatForm.Controller;
using PlatForm.DataLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatForm.Presentation
{
    public partial class frmSupplier : MetroForm
    {
        SupplierController SupplierCtrl = new SupplierController();

        public frmSupplier()
        {
            InitializeComponent();
            dGVSupplier.AutoGenerateColumns = false;
        }

        private void Init()
        {
            var lstSupplier = SupplierCtrl.GetList();
            dGVSupplier.DataSource = lstSupplier;

            var suppl_No = new Binding("Text", lstSupplier, "SUPPL_No");
            txtSUPPL_No.DataBindings.Clear();
            txtSUPPL_No.DataBindings.Add(suppl_No);

            var suppl_Name = new Binding("Text", lstSupplier, "SUPPL_Name");
            txtSUPPL_Name.DataBindings.Clear();
            txtSUPPL_Name.DataBindings.Add(suppl_Name);

            var suppl_Address = new Binding("Text", lstSupplier, "Address");
            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add(suppl_Address);

            txtSUPPL_No.Focus();
        }

        private void frmSupplier_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void txtSUPPL_No_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        public void Delete()
        {
            try
            {
                var msg = MessageBox.Show("Do you want to delete this record?", "Platform", MessageBoxButtons.YesNo);
                if (msg == DialogResult.Yes)
                {
                    SupplierCtrl.Delete(txtSUPPL_No.Text);
                    Init();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not delete this record");
            }
        }

        public void Save()
        {
            var Supplier = new Supplier()
            {
                SUPPL_No = txtSUPPL_No.Text,
                SUPPL_Name = txtSUPPL_Name.Text,
                Address = txtAddress.Text
            };
            try
            {
                var x = SupplierCtrl.Save(Supplier);
                Init();
                if (x == 1)
                    Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not save record");
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                    
                    case Keys.Enter:
                    Save();
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Clear()
        {
            txtSUPPL_No.Text = "";
            txtSUPPL_Name.Text = "";
            txtAddress.Text = "";
            txtSUPPL_No.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Clear();
        }
    }
}
